package org.contact.web;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.contact.model.dto.ContactoDTO;
import org.contact.service.ContactService;
import org.contact.util.BaseUtil;
import org.contact.util.json.JSONArray;
import org.contact.util.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class ContactController extends MultiActionController{
	
	private ContactService contactService;
	public void setContactService(ContactService contactService) {
		this.contactService = contactService;
	}
	
	public ModelAndView init(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("contactos/ContactMain");
	}
	
	public void listarContactos(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		JSONObject root = new JSONObject();
		PrintWriter out = response.getWriter();
		String filterStr = request.getParameter("filter");
		System.out.println(filterStr);
		try {
			JSONArray array = new JSONArray();
			List<ContactoDTO> list = null;
			if(BaseUtil.getDefaultString(filterStr)==null)
				list = contactService.listarTodos(null);
			else {
				JSONObject filter = new JSONObject(filterStr);
				list = contactService.listarTodos(filter.getJSONObjectMap());
			}
			for (ContactoDTO contactDTO : list) {
				JSONObject object = new JSONObject(contactDTO);
				array.put(object);
			}
			root.put("success", true);
			root.put("data", array);
			root.put("total", array.length());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			root.put("success", false);
			
		}
		out.print(root.toString());
		out.flush();
		out.close();
	}
	
	public void guardarContacto(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		JSONObject root = new JSONObject();
		PrintWriter out = response.getWriter();
		
		try {
			ContactoDTO contact = new ContactoDTO();
			contact.setId_contacto(BaseUtil.getDefaultLong(request.getParameter("id_contacto")));
			contact.setNombre(BaseUtil.getDefaultString(request.getParameter("nombre")));
			contact.setApe_pat(BaseUtil.getDefaultString(request.getParameter("ape_pat")));
			contact.setApe_mat(BaseUtil.getDefaultString(request.getParameter("ape_mat")));
			contact.setCalle(BaseUtil.getDefaultString(request.getParameter("calle")));
			contact.setDistrito(BaseUtil.getDefaultString(request.getParameter("distrito")));
			contact.setProvincia(BaseUtil.getDefaultString(request.getParameter("provincia")));
			contact.setEmail1(BaseUtil.getDefaultString(request.getParameter("email1")));
			contact.setEmail2(BaseUtil.getDefaultString(request.getParameter("email2")));
			contact.setTelefono1(BaseUtil.getDefaultString(request.getParameter("telefono1")));
			contact.setTelefono2(BaseUtil.getDefaultString(request.getParameter("telefono2")));
			contact.setMovil1(BaseUtil.getDefaultString(request.getParameter("movil1")));
			contact.setMovil2(BaseUtil.getDefaultString(request.getParameter("movil2")));
			contact.setFax(BaseUtil.getDefaultString("fax"));
			contact.setAnexo1(BaseUtil.getDefaultString(request.getParameter("anexo1")));
			contact.setAnexo2(BaseUtil.getDefaultString(request.getParameter("anexo2")));
			contact.setObs(BaseUtil.getDefaultString(request.getParameter("obs")));
			
			contact.setArea("");
			contact.setCargo("");
			contact.setEmpresa(null);
			
			if(contact.getId_contacto() == null)
				contactService.insertar(contact);
			else contactService.actualizar(contact);
			root.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			root.put("success", false);
			
		}
		out.print(root.toString());
		out.flush();
		out.close();
	}
	
	public void obtenerContacto(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		JSONObject root = new JSONObject();
		PrintWriter out = response.getWriter();
		
		try {
			JSONArray array = new JSONArray();
			ContactoDTO contact = contactService.obtenetXId(new ContactoDTO(BaseUtil.getDefaultLong(request.getParameter("selId"))));
			array.put(new JSONObject(contact));
			root.put("success", true);
			root.put("data", array);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			root.put("success", false);
		}
		out.print(root.toString());
		out.flush();
		out.close();
	}
	
	public void elimninarContacto(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		JSONObject root = new JSONObject();
		PrintWriter out = response.getWriter();
		
		try {
			contactService.eliminar(new ContactoDTO(BaseUtil.getDefaultLong(request.getParameter("selId"))));
			root.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			root.put("success", false);
		}
		out.print(root.toString());
		out.flush();
		out.close();
	}

}
